﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Extensions to the UserAccess model
	/// </summary>
	public partial class UserAccess
	{

		/////////////////////////////////////////////////////////////////
		// Enumerations

		public enum eAction
		{
			UserLoggedOn = 1,
			FailedAuthentication = 2,
		}

		/////////////////////////////////////////////////////////////
		// Attributes

		public eAction Action
		{
			get { return (eAction)this._fdAction; }
			set { this.fdAction = (int)value; }
		}

		/////////////////////////////////////////////////////////////////
		// Static Operations

		public static void UserLoggedOn(DebtChasedEntities ent, User user, string strAddress)
		{

			// Add a new record
			UserAccess userAccess = ent.UserAccesses.CreateObject();

			// Set member variables
			userAccess.UserId = user.UserId;
			userAccess.Timestamp = DateTime.Now;
			userAccess.Action = eAction.UserLoggedOn;
			userAccess.Params = string.Format("addr={0}", strAddress);
			ent.UserAccesses.AddObject(userAccess);

			// Save to entities
			ent.SaveChanges();

		}

		public static void FailedAuthentication(DebtChasedEntities ent, string strAddress, string strUsername, string strPassword)
		{

			// Add a new record
			UserAccess userAccess = ent.UserAccesses.CreateObject();

			// Set member variables
			userAccess.UserId = null;
			userAccess.Timestamp = DateTime.Now;
			userAccess.Action = eAction.FailedAuthentication;
			userAccess.Params = string.Format("addr={0},usr=\"{1}\";pwd=\"{2}\"", strAddress, strUsername, strPassword);
			ent.UserAccesses.AddObject(userAccess);

			// Save to entities
			ent.SaveChanges();

		}

	}

}
